/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.slice;

import com.lingeringdream.luciole.ResourceTable;
import com.lingeringdream.luciole.helper.ResourceHelper;
import com.lingeringdream.luciole.helper.list.NormalListProvider;
import com.lingeringdream.luciole.model.entity.User;
import com.lingeringdream.luciole.model.helper.GetEntityHelper;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 账号管理页面
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/5/4
 */
public class AccountSlice extends AbilitySlice {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x11, AccountSlice.class.getSimpleName());
    private User user;
    private NormalListProvider provider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_slice_normal_list);
        initTitleBar();
        long uid = intent.getLongParam("uid", -1L);
        if (uid != -1L) {
            user = GetEntityHelper.getUser(getContext(), uid);
        } else {
//            TODO 异常处理
            HiLog.info(TAG, "uid is null.");
        }
        if (user != null) {
            initList();
        } else {
//                TODO 提示用户不存在
            HiLog.info(TAG, "user is null.");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (provider != null) {
            provider.clear();
            provider = null;
        }
    }

    /**
     * 初始化titleBar
     */
    private void initTitleBar() {
        Button backButton = findComponentById(ResourceTable.Id_titleBar_back);
        backButton.setClickedListener(component -> terminate());

        Text title = findComponentById(ResourceTable.Id_titleBar_title);
        title.setText("账号管理");
    }

    /**
     * 初始化账户列表
     */
    private void initList() {
        provider = new NormalListProvider(getContext()).setArrow(true).setLine(true);
        String tel = user.getTel();
        provider.addItem("手机号",
                tel == null ? ResourceHelper.getColor(getContext(), ResourceTable.Color_emphasized_default, 0xFFCE0E2D) :
                        ResourceHelper.getColor(getContext(), ResourceTable.Color_commonly_default, 0xFF87CEFF),
                tel == null ? "暂未绑定手机号" : tel.split("-")[1],
                component -> {
                    if (tel != null) {
//                TODO 弹出提示框：更换手机号？
                    } else {
//                TODO 提示用户没有绑定手机号，并且是否绑定手机号
                        HiLog.info(TAG, "tel is null.");
                    }
                });

        String unionId = user.getHuaweiId();
        provider.addItem("华为账号",
                unionId == null ? ResourceHelper.getColor(getContext(), ResourceTable.Color_emphasized_default, 0xFFCE0E2D) :
                        ResourceHelper.getColor(getContext(), ResourceTable.Color_commonly_default, 0xFF87CEFF),
                unionId == null ? "暂未绑定华为账号" : unionId,
                component -> {
                    if (unionId != null) {
//                TODO 弹出提示框：更换华为账号？
                    } else {
//                TODO 提示用户没有绑定华为账号，并且是否绑定华为账号
                        HiLog.info(TAG, "unionId is null.");
                    }
                });

        ListContainer listContainer = findComponentById(ResourceTable.Id_normalList_list);
        listContainer.setItemProvider(provider);
    }
}
